// 测试两日期之间所隔年月
void main() {
  print(CoinTransactionDate.getCoinTransactionDate());
}

class CoinTransactionDate {
  final int year;
  final int month;

  CoinTransactionDate(this.year, this.month);

  @override
  String toString() {
    return "${mapMonth(month)}, $year";
  }

  // calculate: 2021-12 - 2021-11 = [2021-11, 2021-12]
  static List<CoinTransactionDate> getCoinTransactionDate() {
    int fromYear = 2021;
    int fromMonth = 11;
    const int maxMonth = 12;
    DateTime dateTime = DateTime.now();
    int toYear = dateTime.year;
    int toMonth = dateTime.month;

    if (toYear < fromYear) {
      return [CoinTransactionDate(fromYear, fromMonth)];
    }

    if (toYear == fromYear && toMonth <= fromMonth) {
      return [CoinTransactionDate(fromYear, fromMonth)];
    }

    // same year
    if (toYear == fromYear && toMonth > fromMonth) {
      List<CoinTransactionDate> res = [];
      for (int month = fromMonth; month <= toMonth; month++) {
        res.add(CoinTransactionDate(toYear, month));
      }
      return res;
    }

    // diff year
    if (toYear > fromYear) {
      List<CoinTransactionDate> res = [];
      for (int month = fromMonth; month <= maxMonth; month++) {
        res.add(CoinTransactionDate(fromYear, month));
      }
      for (int year = fromYear + 1; year <= toYear; year++) {
        int endMonth = (year == toYear) ? toMonth : maxMonth;
        for (int month = 1; month <= endMonth; month++) {
          res.add(CoinTransactionDate(year, month));
        }
      }
      return res;
    }
    return [CoinTransactionDate(fromYear, fromMonth)];
  }

  static String mapMonth(int month) {
    switch (month) {
      case 1:
        return "Jan";
      case 2:
        return "Feb";
      case 3:
        return "Mar";
      case 4:
        return "Apr";
      case 5:
        return "May";
      case 6:
        return "Jun";
      case 7:
        return "Jul";
      case 8:
        return "Aug";
      case 9:
        return "Sep";
      case 10:
        return "Oct";
      case 11:
        return "Nov";
      case 12:
        return "Dec";
    }
    return "";
  }
}
